package com.courier.controller;

import com.courier.entity.User;
import com.courier.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class LoginController {

    @Autowired
    private UserService userService;

    // 登录接口 - 明文验证
    @PostMapping("/login")
    public LoginResult login(@RequestBody LoginRequest request) {
        // 根据用户名查询用户
        User user = userService.findByUsername(request.getUsername());
        
        // 验证用户名和密码（明文验证）
        if (user != null && request.getPassword().equals(user.getPassword())) {
            // 检查用户状态
            if (user.getStatus() != null && user.getStatus() == 1) {
                // 登录成功，返回用户信息
                return new LoginResult(true, "登录成功", user);
            } else {
                // 用户已禁用
                return new LoginResult(false, "用户已禁用", null);
            }
        } else {
            // 用户名或密码错误
            return new LoginResult(false, "用户名或密码错误", null);
        }
    }

    // 登录请求参数
    static class LoginRequest {
        private String username;
        private String password;

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
    }

    // 登录结果
    static class LoginResult {
        private boolean success;
        private String message;
        private User userInfo;

        public LoginResult(boolean success, String message, User userInfo) {
            this.success = success;
            this.message = message;
            this.userInfo = userInfo;
        }

        public boolean isSuccess() {
            return success;
        }

        public void setSuccess(boolean success) {
            this.success = success;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public User getUserInfo() {
            return userInfo;
        }

        public void setUserInfo(User userInfo) {
            this.userInfo = userInfo;
        }
    }
}